package org.dyk.seamanshousefrontdesk.service.cart;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.dyk.seamanshousefrontdesk.model.Cart;
import org.dyk.seamanshousefrontdesk.model.Goods;
import org.dyk.seamanshousefrontdesk.repository.CartRepository;
import org.dyk.seamanshousefrontdesk.repository.GoodsRepository;
import org.dyk.seamanshousefrontdesk.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CartServiceImpl implements CartService{
    @Autowired
    private CartRepository cartRepository;
    @Autowired
    private GoodsRepository goodsRepository;
    @Autowired
    private UserRepository userRepository;

    /**
     * 购物车页面
     * @param uid
     * @return
     */
    @Override
    public JSONArray findAllByUid(int uid) {
        JSONArray jsonArray = new JSONArray();
        //获取当前用户购物车信息
        List<Cart> allByUid = cartRepository.findAllByUid(uid);
        for (Cart cart:allByUid) {
            JSONObject jsonObject = (JSONObject) JSON.toJSON(goodsRepository.findByGid(cart.getGid()));
            jsonObject.put("cid",cart.getCid());
            jsonObject.put("quantity",cart.getQuantity());
            jsonArray.add(jsonObject);
        }
        return jsonArray;
    }

    @Override
    public Cart findByCid(int cid) {
        return cartRepository.findByCid(cid);
    }

    /**
     * 添加商品到购物车
     * @param gid
     * @return
     */
    @Override
    public boolean insert(int gid, int uid) {
        //获取商品信息，判断商品库存是否为0
        Goods goods = goodsRepository.findByGid(gid);
        if (0 < goods.getGoodsStock()){
            //判断购物车是否已有该商品
            Cart byGid = cartRepository.findByGid(gid);
            if (null == byGid){
                //库存充足，允许添加到购物车
                Cart cart = new Cart();
                cart.setGid(gid);
                cart.setUid(uid);
                cart.setQuantity(1);
                cartRepository.save(cart);
                return true;
            }else {
                byGid.setQuantity(1);
                cartRepository.save(byGid);
                return true;
            }
        }else {
            //库存不足，不允许添加到购物车
            return false;
        }
    }

    /**
     * 删除购物车
     * @param gid
     * @return
     */
    @Override
    public boolean delete(int gid) {
        Cart byGid = cartRepository.findByGid(gid);
        cartRepository.delete(byGid);
        return true;
    }

    /**
     * 修改购物车商品数量
     * @param cid
     * @param goodsNum
     * @return
     */
    @Override
    public boolean updataQuantity(int cid, int goodsNum) {
        //获取购物车信息
        Cart cart = cartRepository.findByCid(cid);
        //判断商品数量是否充足
        Goods goods = goodsRepository.findByGid(cart.getGid());
        if (goodsNum <= goods.getGoodsStock()){
            //修改购物车商品数量
            cart.setQuantity(goodsNum);
            cartRepository.save(cart);
            return true;
        }else {
            return false;
        }

    }
}
